package com.example.mylog.controller;

import com.example.mylog.pojo.User;
import com.example.mylog.service.UserService;
import com.example.mylog.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@Transactional
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/test/{page}/{size}")
    @ResponseBody
    public List<User> getAllUser(@PathVariable("page")int page,@PathVariable("size") int size){
        return userService.getAllUser((page-1)*size,size);
    }
    @RequestMapping("/test1")
    @ResponseBody
    public List<User> getAllUser1(){
        return userService.getUser();
    }
    @RequestMapping("/toIndex")
    public java.lang.String toIndex(){
        return "myindex";
    }
    @RequestMapping("/test2")
    @ResponseBody
    public List<User> test1(@RequestParam("beginTime") Date beginTime, @RequestParam("endTime") Date endTime ){
        List<User> list=new ArrayList<>();
        list=userService.getUserByTime(beginTime,endTime);
      return list;
    }
    @RequestMapping("/test3")
    @ResponseBody
    public String  test3(@RequestParam("beginTime") Date beginTime, @RequestParam("endTime") Date endTime ) throws ParseException {
        SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        String string= simpleDateFormat.format("yyyy-MM-dd");

        return string;
    }
    @RequestMapping("/test4")
    @ResponseBody
    public List<User>  test4(@RequestParam("beginTime") String beginTime, @RequestParam("endTime") String endTime ) throws ParseException {
        SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        Date begindate=simpleDateFormat.parse(beginTime);
        Date endate=simpleDateFormat.parse(endTime);
        if (begindate!=null){
            begindate=DateUtil.getBeginDate(begindate);
        }
        if (endate!=null){
            endate=DateUtil.getEndDate(endate);
        }
        return userService.getUserByTime(begindate,endate);

    }
    @RequestMapping("/test5")
    public String test5(){
        List<User> list=new ArrayList<>();
        User user=new User(11,"ll","ll","ll","ll",new Date());
        User user1=new User(12,"ll","ll","ll","ll",new Date());
        User user2=new User(13,"ll","ll","ll","ll",new Date());
        list.add(user1);
        list.add(user);
        list.add(user2);
        userService.insertMore(list);
        return "myindex";
    }
    @RequestMapping("/test6")
    @ResponseBody
    public List<User> test6(){
        Map<String,Object> map=new HashMap<>();
        map.put("key1",1);
        map.put("key11",11);
        map.put("key12",12);
        return userService.getUsers(map);

    }
}

